//
//  BDAddBusinessInfoViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/12.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "BDAddBusinessInfoViewController.h"
#import "BQActivityView.h"
#import "BDSaveShopInfoRequest.h"
#import "BDUserShopModel.h"
#import "BDBusinessModel.h"
#import "BDChooseIndustryViewController.h"
#import "UIViewController+BDAddBusinessHelper.h"
#import "NSString+Validation.h"

@interface BDAddBusinessInfoViewController ()

@property (weak, nonatomic) IBOutlet UITextField *shopName;
@property (weak, nonatomic) IBOutlet UITextField *subordinateIndustryNameTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextFiled;

@property(nonatomic,copy)NSString * industrycode;

@end

@implementation BDAddBusinessInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.shopName.delegate = self;
    self.subordinateIndustryNameTextFiled.delegate = self;
    self.phoneNumberTextFiled.delegate = self;
    
    [self.titleButton setTitle:@"店铺资料" forState:UIControlStateNormal];
    [self.rightAreaRBtn setTitle:@"下一步" forState:UIControlStateNormal];
    self.rightAreaRBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [self.rightAreaRBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.rightArea.mas_right);
        make.width.mas_equalTo(@50);
        make.centerY.mas_equalTo(self.rightArea.mas_centerY);
        make.height.mas_equalTo(@30);
    }];
    
}

- (void)fillbackIfNeed{
    if (!self.userShopModel) return;
    self.shopName.text = self.userShopModel.appBizShop.name;
    self.phoneNumberTextFiled.text = self.userShopModel.appAuUser.cellphone;
    self.subordinateIndustryNameTextFiled.text = self.userShopModel.appBizShop.industryName;
    self.industrycode = self.userShopModel.appBizShop.industryCode;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)handleNavigationAction:(HSYNavigationActionType)actionType{
    if (actionType == HSYNavActionLeftAreaLeft) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    if (actionType == HSYNavActionRightAreaRight) {
        [self comitBtnClick:nil];
    }
}

- (IBAction)goChooseIndustry1:(id)sender {
    if (self.readOnly) return;
    //选择行业
    BDChooseIndustryViewController *vc = [[BDChooseIndustryViewController alloc] init];
    
    __weak typeof(self)weakSelf = self;
    [vc getIndustrycode:^(NSString *code,NSString *title) {
        weakSelf.industrycode = code;
        weakSelf.subordinateIndustryNameTextFiled.text = title;
    }];
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)commitData{
    [self.view endEditing:YES];
    
    if (!self.shopName.text.length) {
        [self showMessage:@"请填写店铺名称"];
        return ;
    }else if(![self.shopName.text isValidShopShortName]){
        [self showMessage:@"店铺名称限制为5-25个字数"];
        return;
    }
    
    if (!self.phoneNumberTextFiled.text.length) {
        [self showMessage:@"请填写管理员/店主手机号"];
        return ;
    }else if(![self.phoneNumberTextFiled.text isValidPhone]){
        [self showMessage:@"手机号错误，请重新输入"];
        return;
    }
    
    if (!self.industrycode.length) {
        [self showMessage:@"请选择行业"];
        return ;
    }
    
    __weak typeof(self)weakSelf = self;
    [BQActivityView showActiviTy];
    
    BDSaveShopInfoRequest *task = [[BDSaveShopInfoRequest alloc] init];
    task.auid = [BDUserAccount instatce].auid;
    task.cellphone = self.phoneNumberTextFiled.text;
    task.shopName = self.shopName.text;
    task.industryCode = self.industrycode;
    
    [BDNetworkManager POST:task completionHander:^(id responseObject, NSString *resultString) {
        [BQActivityView hideActiviTy];
        
        if (responseObject) {
            BDUserShopModel *model = [BDUserShopModel mj_objectWithKeyValues:responseObject];
            BDBusinessModel *business = [model toBusinessModel];
            //go to next step
            [weakSelf nextStepWihBusinessModel:business  userShopModel:weakSelf.userShopModel readOnly:weakSelf.readOnly];
        }else{
            if (resultString) [weakSelf showMessage:resultString];
        }
    }];
}

- (IBAction)comitBtnClick:(id)sender {
    [self handleNext];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    BOOL b = [super textFieldShouldBeginEditing:textField];
    
    if (b && textField == self.subordinateIndustryNameTextFiled) {
        b = NO;
    }
    return b;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
